package com.witframework.components.tag;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang.StringUtils;
import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;

import com.witframework.PropertiesConstants;
import com.witframework.core.ApplicationProperties;

public class DataSetAuthSelectTreeTag extends TagSupport{		
	private String treeid;
	private String data;//数据源，可以是实体的类名或数据库的表名，或是sql查询语句
	private String data2;//数据源，可以是实体的类名或数据库的表名，或是sql查询语句	
	private String root;//指向父节点的属性名称，对于PAGE和SQL类型则忽略该属性
	private String parentid;//指向父节点的属性名称，对于PAGE和SQL类型则忽略该属性	
	private String parentid2;//指向父节点的属性名称，对于PAGE和SQL类型则忽略该属性	
	private String nodeid;//对应的熟悉名称(树节点的ID)，对于PAGE类型则忽略该属性
	private String nodeid2;	
	private String nodelabel;//上面对应的数据字段名称(树节点的label文本)，对于PAGE类型则忽略该属性
	private String nodelabel2;		
	/**
	 * 数据源类型,table:数据库表；class:实体类名(通过对应的service查找数据),默认的数据源类型；
	 * sql:sql 查询语句.如果为sql类型，则filters属性的设置是无效的
	 */
	private String datatype="class";	
	private String multiple;//是否允许多选，对于PAGE类型则忽略该属性
	private String selected;//尚未完全实现，默认的选中项	
	private String onlyLeaf="true";
	
	private String width;//对话框宽度
	private String height;//对话框高度	
	@Override
	public int doStartTag() throws JspException {
		StringBuffer dataUrl=new StringBuffer("dataSetAuthSelectTreeController!treepage"+ApplicationProperties.getProperty(PropertiesConstants.WIT));//数据列表的URL
		if(StringUtils.isNotEmpty(datatype)&&datatype.equalsIgnoreCase("page")){
			dataUrl=new StringBuffer(data);
		}
		HttpServletRequest request=(HttpServletRequest)pageContext.getRequest();
		JspWriter writer = pageContext.getOut();		
		
		if(dataUrl.toString().startsWith("/")&&!dataUrl.toString().startsWith(request.getContextPath())) dataUrl=new StringBuffer(request.getContextPath()+dataUrl);
		dataUrl.append((dataUrl.indexOf("?")>0?"&":"?")+"treeid="+treeid+"&data="+getData());
		if(StringUtils.isNotEmpty(root)) dataUrl.append("&root="+root);
		if(StringUtils.isNotEmpty(datatype)||!datatype.equalsIgnoreCase("page")){	
			if(StringUtils.isNotEmpty(data2)) dataUrl.append("&data2="+getData2());
			if(StringUtils.isNotEmpty(parentid)) dataUrl.append("&parentid="+parentid);
			if(StringUtils.isNotEmpty(parentid2)) dataUrl.append("&parentid2="+parentid2);
			if(StringUtils.isNotEmpty(nodeid)) dataUrl.append("&nodeid="+nodeid);
			if(StringUtils.isNotEmpty(nodeid2)) dataUrl.append("&nodeid2="+nodeid2);
			if(StringUtils.isNotEmpty(nodelabel))	dataUrl.append("&nodelabel="+nodelabel);
			if(StringUtils.isNotEmpty(nodelabel2))	dataUrl.append("&nodelabel2="+nodelabel2);			
			if(StringUtils.isNotEmpty(selected))	dataUrl.append("&selected="+selected);
			if(StringUtils.isNotEmpty(datatype)) dataUrl.append("&datatype="+datatype);	
			if(StringUtils.isNotEmpty(height)) dataUrl.append("&height="+height);	
			if(StringUtils.isNotEmpty(width)) dataUrl.append("&width="+width);	
		}		
		if(StringUtils.isNotEmpty(multiple))	dataUrl.append("&multiple="+multiple);	
		if(StringUtils.isNotEmpty(onlyLeaf))	dataUrl.append("&onlyLeaf="+onlyLeaf);
		StringBuffer html=new StringBuffer("<iframe");		
		html.append(" src=\""+dataUrl+"\" width=\""+width+"\" frameborder=\"0\" height=\""+height+"\"></iframe>");		
		try {			
			writer.println(html);
		} catch (IOException e) {
			throw new JspException("Print/Flush in LoadingTag tag error:" + e);
		}
		return (EVAL_BODY_INCLUDE);
	}

	public void setDatatype(String datatype) {
		this.datatype = datatype;
	}

	

	public void setMultiple(String multiple) {
		this.multiple = multiple;
	}

	public void setSelected(String selected) {
		this.selected = selected;		
		try {
			this.selected=(String) ExpressionEvaluatorManager.evaluate("selected",selected,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}

	public void setData(String data) {
		this.data = data;
		try {
			this.data=(String) ExpressionEvaluatorManager.evaluate("data",data,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}



	public void setData2(String data2) {
		this.data2 = data2;
		try {
			this.data2=(String) ExpressionEvaluatorManager.evaluate("data2",data2,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}



	


	public void setNodeid(String nodeid) {
		this.nodeid = nodeid;
	}





	public void setNodelabel(String nodelabel) {
		this.nodelabel = nodelabel;
	}


	public void setParentid(String parentid) {
		this.parentid = parentid;
	}



	public void setRoot(String root) {
		this.root = root;
	}




	public void setOnlyLeaf(String onlyLeaf) {
		this.onlyLeaf = onlyLeaf;
	}



	public void setNodeid2(String nodeid2) {
		this.nodeid2 = nodeid2;
	}



	public void setNodelabel2(String nodelabel2) {
		this.nodelabel2 = nodelabel2;
	}
	public String getData() {
		try {
			return java.net.URLEncoder.encode(data,"utf-8");
		} catch (Exception e1) {			
		}
		return data;
	}
	public String getData2() {
		try {
			return java.net.URLEncoder.encode(data2,"utf-8");
		} catch (Exception e1) {			
		}
		return data2;
	}



	public String getRoot() {
		return root;
	}



	public String getParentid() {
		return parentid;
	}



	

	public String getNodeid() {
		return nodeid;
	}



	public String getNodeid2() {
		return nodeid2;
	}




	public String getNodelabel() {
		return nodelabel;
	}



	public String getNodelabel2() {
		return nodelabel2;
	}


	public String getDatatype() {
		return datatype;
	}



	public String getMultiple() {
		return multiple;
	}



	public String getSelected() {
		return selected;
	}



	public String getOnlyLeaf() {
		return onlyLeaf;
	}
	public String getTreeid() {
		return treeid;
	}

	public void setTreeid(String treeid) {
		this.treeid = treeid;
		try {
			this.treeid=(String) ExpressionEvaluatorManager.evaluate("treeid",treeid,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}

	public void setWidth(String width) {
		this.width = width;
		try {
			this.width=(String) ExpressionEvaluatorManager.evaluate("width",width,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
		
	}
	public void setHeight(String height) {
		this.height = height;
		try {
			this.height=(String) ExpressionEvaluatorManager.evaluate("height",height,String.class,this,pageContext);
		} catch (JspException e) {
			e.printStackTrace();
		}
	}

	public void setParentid2(String parentid2) {
		this.parentid2 = parentid2;
	}

	

}
